package com.sugon.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevAlarmBoolDao;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.entity.DevAlarmBoolEntity;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.service.DevAlarmBoolService;
import com.sugon.utils.RRException;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备布尔类型报警码对应表Service实现类
 *
 * @author sugon
 * @date 2020-11-30 19:54:12
 */
@Service("devAlarmBoolService")
public class DevAlarmBoolServiceImpl implements DevAlarmBoolService {
    @Autowired
    private DevAlarmBoolDao devAlarmBoolDao;
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevAlarmBoolEntity queryObject(String id) {
        return devAlarmBoolDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevAlarmBoolEntity> queryList(Map<String, Object> map) {
        return devAlarmBoolDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devAlarmBoolDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param devAlarmBool 实体
     * @return
     */
    @Override
    public int save(DevAlarmBoolEntity devAlarmBool) {
        devAlarmBool.setId(RandomUtil.randomString(32));
        devAlarmBool.setDelFlag(1);
        return devAlarmBoolDao.save(devAlarmBool);
    }

    /**
     * update
     *
     * @param devAlarmBool 实体
     * @return
     */
    @Override
    public int update(DevAlarmBoolEntity devAlarmBool) {
        return devAlarmBoolDao.update(devAlarmBool);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devAlarmBoolDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devAlarmBoolDao.deleteBatch(ids);
    }

    /**
     * imports
     *
     * @param file
     * @return
     */
    @Override
    public int imports(MultipartFile file) {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        List<String[]> list = ExcelImport.getExcelData(file);
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            int size = i + 1;
            if (size < list.size()) {
                String[] ftArr = list.get(size);
                // 确保长度为4
                ftArr = ArrayUtil.resize(ftArr, 4);
                Map<String, Object> param = new HashMap<>(2);
                param.put("devLastCode", ftArr[0]);
                param.put("alarmAddr", ftArr[1]);
                int count = devAlarmBoolDao.queryTotal(param);
                if (count > 0) {
                    continue;
                }
                DeviceInfoEntity deviceInfo = deviceInfoDao.getInfoByDevLastCode(ftArr[0]);
                if (deviceInfo == null) {
                    continue;
                }
                DevAlarmBoolEntity alarm = new DevAlarmBoolEntity();
                // 设备底层码
                alarm.setDevLastCode(ftArr[0]);
                alarm.setDevId(deviceInfo.getId());
                // 设置报警地址
                alarm.setAlarmAddr(ftArr[1]);
                // 设置报警消息
                alarm.setMsg(ftArr[2]);
                // 设置报警码
                alarm.setCode(ftArr[3]);
                num += save(alarm);
            }
        }
        return num;
    }
}
